#include "homepage.h"     // 主页头文件
#include "ui_homepage.h"  // 界面文件
#include "globalval.h"    // 全局变量文件

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);  // 设置用户界面

    // 实例化五个子界面
    m0Wig = new M0Wig;
    modbusWig = new ModbusWig;
    mbappWig = new MbappWig;
    setWig = new SetWig;
    history = new History;

    // 将五个子界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(setWig);
    ui->stackedWidget->addWidget(modbusWig);
    ui->stackedWidget->addWidget(mbappWig);
    ui->stackedWidget->addWidget(m0Wig);
    ui->stackedWidget->addWidget(history);

    // 连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    // MQTT相关设置
    MqttHandler *mqtt = new MqttHandler;  // 创建MQTT处理实例
    GlobalVal::mqtt = mqtt;  // 设置全局MQTT对象
    mqtt->setHost(QHostAddress("192.168.50.80"));  // 设置MQTT主机地址
    mqtt->setPort(1883);  // 设置MQTT端口
    mqtt->connectToHost();  // 连接到MQTT服务器

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     modbusWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     m0Wig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     setWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     mbappWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     history, SLOT(dataUpSlot(QByteArray)));//dataUpSignal
}

//void HomePage::paintEvent(QPaintEvent* event)
//{
//    QPainter painter(this);
//    QPixmap pic(":/new/prefix1/homepage.png");
//    painter.drawPixmap(0,0,this->width(),this->height(),pic);
//}

HomePage::~HomePage()
{
    delete ui;  // 删除界面对象
}
